﻿using Daemon.Model;
using Microsoft.AspNetCore.Mvc;
using Daemon.Repository.Contract;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Daemon.Model.Entities;
using Daemon.Common;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System;
using Microsoft.AspNetCore.SignalR;
using DaemonAPI.Hubs;
using Newtonsoft.Json;
using Daemon.Data.Substructure.Enums;
using Daemon.Common.Exceptions;
using Daemon.Infrustructure.Contract;

namespace DaemonAPI
{
	[ApiController]
	[Route("api/[controller]/[action]")]
	public class TakeAwayUserOrderController : BaseApiController<TakeAwayUserOrder, ITakeAwayUserOrderRepository>
	{
		private readonly ITakeAwayIntegeralSettingRepository _takeAwayIntegeralSettingRepository;

		private readonly ITakeAwayAccountTransactionRepository _takeAwayAccountTransactionRepository;

		private readonly ITakeAwayUserAccountRepository _takeAwayUserAccountRepository;

		private readonly IUnitOfWork _unitOfWork;

		private readonly ApiDBContent _dbContext;

		public TakeAwayUserOrderController(ApiDBContent dbContext, IUnitOfWork unitOfWork, ITakeAwayAccountTransactionRepository takeAwayAccountTransactionRepository, ITakeAwayUserAccountRepository takeAwayUserAccountRepository, ITakeAwayIntegeralSettingRepository takeAwayIntegeralSettingRepository, ITakeAwayUserOrderRepository repository, IHttpContextAccessor httpContextAccessor) : base(repository, httpContextAccessor)
		{
			_takeAwayIntegeralSettingRepository = takeAwayIntegeralSettingRepository;
			_takeAwayAccountTransactionRepository = takeAwayAccountTransactionRepository;
			_takeAwayUserAccountRepository = takeAwayUserAccountRepository;
			_dbContext = dbContext;
			_unitOfWork = unitOfWork;
		}

		[HttpPost]
		[Route("")]
		public ResultModel CancelOrder(int orderId)
		{
			var userOrder = _repository.FindById(orderId);
			if (userOrder.Status > (int)UserOrderStatusEnum.Sending)
			{
				throw new BusinessException((int)HttpStatusCode.BadRequest, "");
			}

			userOrder.Status = (int)UserOrderStatusEnum.Cancel;
			try
			{
				_unitOfWork.BeginTransaction(_dbContext);
				_repository.UpdateByDBContext(userOrder, _dbContext);
				_takeAwayAccountTransactionRepository.AddByDBContext(new TakeAwayAccountTransaction() { });
				_unitOfWork.CommitTransaction(_dbContext);
			}
			catch
			{
				throw new BusinessException((int)HttpStatusCode.BadRequest, "");
			}
			finally
			{
				_unitOfWork.RollbackTransaction(_dbContext);
			}

			return new ResultModel((int)HttpStatusCode.OK, null);
		}

		[HttpGet]
		[Route("")]
		public ResultModel<TakeAwayUserOrder, ITakeAwayUserOrderRepository> GetOrderDetailById(int id)
		{
			_repository.SetRelationships(new List<Relationship>() { new Relationship() { Name = "OrderDetail" } });
			var userOrderDetail = _repository.IncludeRelationships(_repository.FindAll().Where(r => r.Id == id));
			return new ResultModel<TakeAwayUserOrder, ITakeAwayUserOrderRepository>(System.Net.HttpStatusCode.OK, null, userOrderDetail);
		}

		[HttpGet]
		[Route("")]
		public ResultModel<TakeAwayUserOrder, ITakeAwayUserOrderRepository> GetAllByUserId(int userId)
		{
			_repository.SetRelationships(new List<Relationship>() { new Relationship() { Name = "OrderDetail" } });
			var userOrderDetail = _repository.IncludeRelationships(_repository.FindAll().Where(r => r.UserId == userId).OrderByDescending(r => r.AddTime));
			return new ResultModel<TakeAwayUserOrder, ITakeAwayUserOrderRepository>(System.Net.HttpStatusCode.OK, null, userOrderDetail);
		}

		[HttpPost]
		[Route("")]
		public override ResultModel<TakeAwayUserOrder, ITakeAwayUserOrderRepository> Add([FromBody] List<TakeAwayUserOrder> list)
		{
			IEnumerable<TakeAwayUserOrder> result = null;
			_repository.SetRelationships(new List<Relationship>() { new Relationship() { Name = "OrderDetail" } });
			try
			{
				_unitOfWork.BeginTransaction(_dbContext);
				result = _repository.AddRangeWithRelationships(list, _dbContext);
			}
			catch
			{
				_unitOfWork.RollbackTransaction(_dbContext);
				return new ResultModel<TakeAwayUserOrder, ITakeAwayUserOrderRepository>(HttpStatusCode.BadRequest, null);
			}
			finally
			{
				_unitOfWork.CommitTransaction(_dbContext);
			}

			return new ResultModel<TakeAwayUserOrder, ITakeAwayUserOrderRepository>(HttpStatusCode.Created, result);
		}
	}
}